﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author Oplink OMS
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webems.modules.log.service;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.common.utils.DateUtils;
import com.oplink.webems.modules.log.dao.CommLogDao;
import com.oplink.webems.modules.log.entity.CommLog;



/**
 * 告警Service
 * @author Oplink OMS
 * @version 2014-09-09
 */
@Component
@Transactional
public class CommLogService {
	 public static final String addCard  ="添加板卡";
	 public static final String delCard  ="删除板卡";
	 public static final String desc     ="板卡描述";
	 public static final String get      ="获取信息";
	 public static final String getMap   ="批量获取信息";
	 public static final String scanCards="扫描板卡设备";
	 public static final String setMap   ="批量配置";
	 
	 
	 public static final Map<String, String> commLogDesc = new HashMap<String, String>(){
		private static final long serialVersionUID = 4320616647095918024L;
		{
			put("addCard",addCard);
			put("delCard",delCard);
			put("desc",desc);
			put("get",get);
			put("getMap",getMap);
			put("scanCards",scanCards);
			put("setMap",setMap);
		}
	 };
	 
	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CommLogService.class);
	
	@Autowired
	private CommLogDao commLogDao;
	
	public CommLog get(Long id) {
		return commLogDao.findOne(id);
	}
	
	public Page<CommLog> find(Page<CommLog> page, CommLog commLog, String beginTime, String endTime) {
		DetachedCriteria dc = commLogDao.createDetachedCriteria();
		//添加查询条件
        if (StringUtils.isNotEmpty(commLog.getIp())){
			dc.add(Restrictions.eq("ip", commLog.getIp()));
		}
        if (StringUtils.isNotEmpty(commLog.getSrc())){
			dc.add(Restrictions.like("src", "%"+commLog.getSrc()+"%"));
		}
		if (null != commLog.getType()
				&& StringUtils.isNotEmpty(commLog.getType().toString())) {
			dc.add(Restrictions.eq("type", commLog.getType()));
		}
        if (StringUtils.isNotEmpty(commLog.getDesci())){
			dc.add(Restrictions.like("desci", "%"+commLog.getDesci()+"%"));
		}
        if (StringUtils.isNotEmpty(commLog.getParams())){
			dc.add(Restrictions.like("params", "%"+commLog.getParams()+"%"));
		}
        if (StringUtils.isNotEmpty(beginTime)){
        	Calendar bg = DateUtils.getCalendar(beginTime);
        	if(bg!=null){
        		dc.add(Restrictions.ge("timei",bg));
        	}
			//dc.add(Restrictions.like("timei", "%"+commLog.getTimei()+"%"));
		}
        if (StringUtils.isNotEmpty(endTime)){
        	Calendar end = DateUtils.getCalendar(endTime);
        	if(end!=null){
        		dc.add(Restrictions.le("timei",end));
        	}
		}
		dc.add(Restrictions.eq(CommLog.DEL_FLAG, CommLog.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return commLogDao.find(page, dc);
	}
	
	/**
	  * @function 创建查询
	  **/
	public Page<CommLog> getUserCommLog(Page<CommLog> page, CommLog commLog, String beginTime, String endTime) {
		DetachedCriteria dc = commLogDao.createDetachedCriteria();
		//添加查询条件
        if (StringUtils.isNotEmpty(commLog.getIp())){
			dc.add(Restrictions.like("ip", "%"+commLog.getIp()+"%"));
		}
        if (StringUtils.isNotEmpty(commLog.getSrc())){
			dc.add(Restrictions.like("src", "%"+commLog.getSrc()+"%"));
		}
		if (null != commLog.getType()
				&& StringUtils.isNotEmpty(commLog.getType().toString())) {
			dc.add(Restrictions.like("type", "%"+commLog.getType()+"%"));
		}
        if (StringUtils.isNotEmpty(commLog.getDesci())){
			dc.add(Restrictions.like("desc", "%"+commLog.getDesci()+"%"));
		}
        if (StringUtils.isNotEmpty(commLog.getParams())){
			dc.add(Restrictions.like("params", "%"+commLog.getParams()+"%"));
		}
        if (StringUtils.isNotEmpty(beginTime)){
        	Calendar bg = DateUtils.getCalendar(beginTime);
        	if(bg!=null){
        		dc.add(Restrictions.ge("timei",bg));
        	}
			//dc.add(Restrictions.like("timei", "%"+commLog.getTimei()+"%"));
		}
        if (StringUtils.isNotEmpty(endTime)){
        	Calendar end = DateUtils.getCalendar(endTime);
        	if(end!=null){
        		dc.add(Restrictions.le("timei",end));
        	}
		}
		dc.add(Restrictions.eq(CommLog.DEL_FLAG, CommLog.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return commLogDao.find(page, dc);
	}
	
	public CommLog getCommLog(Long id) {
		return commLogDao.findOne(id);
	}

	public void saveCommLog(CommLog entity) {
		commLogDao.save(entity);
	}
	
	public void deleteCommLog(Long id) {
		commLogDao.delete(id);
	}
	
	@Autowired
	public void setCommLogDao(CommLogDao commLogDao) {
		this.commLogDao = commLogDao;
	}	
}
